package com.superman.czx.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.superman.czx.common.R;
import com.superman.czx.service.DictService;
import entity.Dict;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;

@Controller
@Slf4j
public class testController {

    @Autowired
    private DictService dictService;
    @RequestMapping("/index")
    public String sayHello() {


        return "aa";
    }
    @RequestMapping("dict")
    public List<Dict> searchDict(){
        List<Dict> search = dictService.search();
        return search;
    }
    @RequestMapping("excel")
    public void exportByTemplate(HttpServletResponse response) throws IOException {

        try {
            //主要分为加载资源->读取资源->写入响应流
            ClassPathResource classPathResource = new ClassPathResource("/templates/excel/aaa.xlsx");
            InputStream inputStream = classPathResource.getInputStream();
            //Workbook workbook = new HSSFWorkbook(inputStream);
            //xls xlsx不用版本用不同workbook  03 07版excel问题
            XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode("easyexcel.xlsx", "utf-8"));
            response.setHeader("Access-Control-Expose-Headers", "content-Disposition");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("excel2")
    public void exportByTemplate2(HttpServletResponse response) throws IOException {
//        导出其实就是把文件读成输入流，然后new byte(流) 转成字节码 再用输入流read
//        再用输出流out 写出去就行了  期间需要注意设置头信息 和关闭流操作

        try {
            String templatePath = "/templates/excel/aaa.xlsx";
            InputStream fis = this.getClass().getResourceAsStream(templatePath);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("批量导入标准模板.xls", "utf-8"));
            response.addHeader("Content-Length", "" + buffer.length);
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.info(e.getMessage());
        }
    }

}
